﻿using System.Collections.Generic;
using System.Linq;

using Cinema.BL.Interfaces.DTOs;
using Cinema.BL.Interfaces.Managers;
using Cinema.Model.Interfaces;
using Cinema.Model.Entities;
using System;

namespace Cinema.BL.Managers
{
    public class MovieManager : IMovieManager
    {
        private IUnitOfWork _unitOfWork;

        public MovieManager(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        public IEnumerable<MovieDTO> GetList()
        {
            return _unitOfWork.MovieRepository.Read()
                .Select(x => new MovieDTO()
                {
                    Id = x.Id,
                    Director = x.Director,
                    Title = x.Title,
                    Year = x.Year
                }).ToList();
        }

        public MovieDTO GetMovieById(int id)
        {
            var results = _unitOfWork.MovieRepository.Read().Where(x => x.Id.Equals(id)).Select(x => new MovieDTO()
            {
                Id = x.Id,
                Director = x.Director,
                Title = x.Title,
                Year = x.Year
            });

            if (results.Count() > 0)
                return results.First();

            return null;
        }

        public MovieDTO InserteMovie(MovieDTO movie)
        {
            try
            {
                Movie insertedMovie = _unitOfWork.MovieRepository.Create(new Movie()
                {
                    Director = movie.Director,
                    Title = movie.Title,
                    Year = movie.Year
                });
                _unitOfWork.SaveChanges();

                return new MovieDTO()
                {
                    Director = insertedMovie.Director,
                    Id = insertedMovie.Id,
                    Title = insertedMovie.Title,
                    Year = insertedMovie.Year
                };
            }
            catch (Exception)
            {
                return null;
            }
        }

        public bool DeleteMovie(int id)
        {
            try
            {
                _unitOfWork.MovieRepository.DeleteById(id);
                _unitOfWork.SaveChanges();

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public MovieDTO UpdateMovie(MovieDTO movie)
        {
            try
            {
                Movie updatedMovie = _unitOfWork.MovieRepository.Update(new Movie()
                {
                    Director = movie.Director,
                    Id = movie.Id,
                    Title = movie.Title,
                    Year = movie.Year
                });
                _unitOfWork.SaveChanges();

                return new MovieDTO()
                {
                    Director = updatedMovie.Director,
                    Id = updatedMovie.Id,
                    Title = updatedMovie.Title,
                    Year = updatedMovie.Year
                };
            }
            catch (Exception)
            {
                return null;
            }
        }

    }
}
